import os
import requests
from lxml import etree
import random

if __name__ == '__main__':
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BookSystem.settings')
    import django

    django.setup()
    from book.models import Book

    url = 'https://www.qimao.com/paihang/boy/hot/date'
    response = requests.get(url)
    page_text = response.text
    tree = etree.HTML(page_text)
    li_list = tree.xpath('//*[@id="__layout"]/div/div[3]/div/div[2]/div[2]/div[2]/div/ul/li')
    count = 0
    book_dict = {}
    for li in li_list:
        count += 1
        # ./div/div[1]/a
        src = li.xpath(f'../li[{count}]/div/div[1]/a/img/@src')[0]
        title = li.xpath(f'../li[{count}]/div/div[2]/div/a/text()')[0]
        desc = li.xpath(f'../li[{count}]/div/div[2]/span[2]/text()')[0]
        detail_url = li.xpath(f'../li[{count}]/div/div[1]/a/@href')[0]
        detail_status = li.xpath(f'../li[{count}]/div/div[2]/span[1]/em[4]/text()')[0]
        count_article = li.xpath(f'../li[{count}]/div/div[2]/span[1]/em[6]/text()')[0][:-2]
        # print(count_article)
        # book_dict[title] = {
        #     'src': src,
        #     'url': detail_url,
        #     'word_count': count_article
        # }
        set_author_list = set([random.choice([i for i in range(1, 6)]) for i in range(random.randint(1, 6))])
        book_obj = Book.objects.create(name=title, price=random.randint(99, 999), desc=desc,
                                       publish_id=random.randint(1, 4), image=src,
                                       url=detail_url, word_count=count_article)
        book_obj.author.set(list(set_author_list))
    # for k, v in book_dict.items():
    #     Book.objects.filter(name=k).update(url=v['url'], word_count=v['word_count'])
